package com.example.stars_project_android.index;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.LinearLayout;

import com.example.stars_project_android.R;
import com.example.stars_project_android.personalCenter.User;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息界面
 */
public class Messages extends AppCompatActivity implements View.OnClickListener {

    //定义变量的属性
    private TabLayout tabLayout;
    private ViewPager2 vp2;
    //底部导航栏
//    private LinearLayout index, msg, add, talk, user;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messages);

        //获取控件对象
        getView();
        //准备fragment数据源
        List<Fragment> fragments = setData();
        //实例化适配器对象
//        MyAdapter adapter1 = new MyAdapter(
//                fragments,this
//        );
//        //绑定适配器
//        vp2.setAdapter(adapter1);
        //关联TabLayout和ViewPager2
        TabLayoutMediator mediator = new TabLayoutMediator(
                tabLayout,
                vp2,
                new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        switch (position){
                            case 0://第一个标签
                                tab.setText("关注");
                                break;
                            case 1://第二个标签
                                tab.setText("未关注");
                                break;
                        }
                    }
                }
        );
        mediator.attach();//实现方法
        //导航栏选中加粗
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if(tab == null || tab.getText() == null){
                    return;
                }
                String trim = tab.getText().toString().trim();
                SpannableString spStr = new SpannableString(trim);
                StyleSpan styleSpan_B = new StyleSpan(Typeface.BOLD);
                spStr.setSpan(styleSpan_B,0,trim.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                tab.setText(spStr);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if(tab == null || tab.getText() == null){
                    return;
                }
                String trim = tab.getText().toString().trim();
                SpannableString spStr = new SpannableString(trim);
                StyleSpan styleSpan_B = new StyleSpan(Typeface.NORMAL);
                spStr.setSpan(styleSpan_B,0,trim.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                tab.setText(spStr);

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private List<Fragment> setData() {
        List<Fragment> fragments = new ArrayList<>();
//        fragments.add(new MessageFragment());
//        fragments.add(new MessageFragment());
        return fragments;
    }

    private void getView() {
        tabLayout = findViewById(R.id.tabLayout);
        vp2 = findViewById(R.id.vp2);
//        index = findViewById(R.id.index);
//        msg = findViewById(R.id.msg);
//        add = findViewById(R.id.add);
//        talk = findViewById(R.id.talk);
//        user = findViewById(R.id.user);
//        index.setOnClickListener(this);
//        msg.setOnClickListener(this);
//        add.setOnClickListener(this);
//        talk.setOnClickListener(this);
//        user.setOnClickListener(this);
    }

    //底部导航栏点击跳转页面功能
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.index:
//                Intent intent1 = new Intent(this, Index.class);
//                startActivity(intent1);
//                break;
//            case R.id.msg:
//                Intent intent2 = new Intent(this, Guestbook.class);
//                startActivity(intent2);
//                break;
//            case R.id.add:
//                Intent intent3 = new Intent(this, RoomDetail.class);
//                startActivity(intent3);
//                break;
//            case R.id.user:
//                Intent intent4 = new Intent(this, User.class);
//                startActivity(intent4);
//                break;
            default:
                break;
        }
    }
}